﻿<template>
  <div id="order-item" ref="order" class="order-item">
    <table>
      <tr>
        <th style="width:100%;" colspan="6">
          <div class="c-h-l">
            <span>订单编号：{{orderAll.orderId}}</span>
            <span>{{orderAll.addtime}}</span>
          </div>
          <div class="c-h-r">
            <span class="store-name">{{orderAll.storeName}}</span>
            <span class="iconfont ico-concant connect-user" @click="onContactShop(orderAll.storeId, orderAll.id, orderAll.goodsInfoList[0].goods_name, orderAll.goodsInfoList[0].goods_mainphoto_path)"></span>
          </div>
          <div class="delBtn" 
            v-if="orderAll.orderStatus == '0' || orderAll.orderStatus == '25' ||  
                  orderAll.orderStatus == '40' || orderAll.orderStatus == '50' || (orderAll.orderStatus == '10' && orderAll.isAlready == '-1') " 
            @click="deleteOrder(orderAll.id)">
          </div>
        </th>
      </tr>
    </table>
    <div class="clearfix">
      <table style="float: left; width: 62.5%; margin: 0">
        <!--订单商品信息  -->
        <tbody v-for="(list, infoIndex) in goodsInfoList" :key="infoIndex">
          <tr v-for="(gsp, gspIndex) in list.goodsGsp" :key="gspIndex">
            <!-- 货品 -->
            <td style=" width: 60%">
              <div class="cols1">
                <div class="clearfix">
                  <div class="shopImg" @click="goGoodsDetail(list.goods_id)">
                    <img class="minimg" v-lazy="list.goods_mainphoto_path" /> 
                  </div>
                  <div class="shop-r">
                    <div class="shop-r-t" @click="goGoodsDetail(list.goods_id)">
                      <span>{{list.goods_name +" "+ list.goodsSerial}}</span>
                      <br>
                      <span class="goods-type" v-if="list.goodsType == 1">伙拼</span>
                    </div>
                    <div class="shop-r-b">
                      <span class="shop-r-b-l">颜色：{{gsp.goodsSpecContent[0]}}</span>
                      <span class="shop-r-b-r">尺码：{{gsp.goodsSpecContent[1]}}</span>      
                    </div>
                  </div>
                </div>
              </div>
            </td>
            <td style=" width: 20%" class="text-c t-c">￥{{gsp.goodsSinglePrice | currency}}</td>
            <td style=" width: 20%" class="text-c2 t-c">{{gsp.count}}</td>
          </tr>
        </tbody>
      </table>
      <table style="float: left; width: 37.5%; margin: 0" :height="tabHeight">
        <tr v-for="(list, infoIndex) in goodsInfoList" :key="infoIndex" v-if="infoIndex == 0">
          <td style=" width: 30%" class="t-c text-c" :rowspan="skuLen">
            <span>￥{{orderAll.totalPrice | currency}}</span>
            <!-- <span>（含运费：￥{{orderAll.shipPrice || 0 | currency}}）</span> -->
          </td>
          <!--根据订单状态，判断买家提示  -->
          <td style=" width: 30%" class="t-c" :rowspan="skuLen">
            <div>
              <!--待付款且拼单未成功  -->
              <div class="text-c" v-if="orderAll.orderStatus == 10 && ((orderAll.isAlready == '0' || list.goodsType == '0'))">等待买家付款</div>
              <!--待付款且拼单已完成  -->
              <div class="text-c" v-if="orderAll.orderStatus == 10 && orderAll.isAlready != '0' && list.goodsType == '1'">未成团</div> 
              <!-- 已付款30% -->
              <div class="text-c" v-if="orderAll.orderStatus == 18">已付款30%</div>
              <!--待发货  -->
              <div class="text-c" v-if="orderAll.orderStatus == 20">买家已付款</div>
              <!--订单取消 退款审核中 已退款 -->
              <div class="text-c" v-if="orderAll.orderStatus == 21 || orderAll.orderStatus == 22 || orderAll.orderStatus == 23">退款审核中</div>
              <div class="text-c" v-if="orderAll.orderStatus == 0 || orderAll.orderStatus == 25">关闭交易</div> 
              <!--待收货  -->
              <div class="text-c" v-if="orderAll.orderStatus == 30">卖家已发货</div>
              <!--去评价 -->
              <div class="text-c" v-if="orderAll.orderStatus == 40">买家已收货</div>
              <!--已评价 -->
              <div class="text-c" v-if="orderAll.orderStatus == 50">交易完成</div>
              <div class="text-c2 f-s goDetail" style="margin-top:10px;" @click="goOrderDetail(orderAll.id)">订单详情</div>
            </div>
          </td>
          <!--根据不同订单状态，显示操作按钮  -->
          <td style=" width: 30%" :rowspan="skuLen">
            <!--待付款  -->
            <div class="t-c" v-if="orderAll.orderStatus == 10 || orderAll.orderStatus == 18">
              <button class="btn button" 
                @click="$router.push(`/payment?ids=${orderAll.id}`)" 
                v-if="(orderAll.isAlready == '0' || list.goodsType == '0') && !orderAll.orderExplain"
                style="margin-bottom: 20px">付款</button>
              <div class="text-c2 del-order" @click="delOrder">取消订单</div>
            </div>
            <!--待发货  -->
            <div class="t-c" v-if="orderAll.orderStatus == 20"></div>
            <!--待收货  -->
            <!-- <div class="t-c" v-if="orderAll.orderStatus == 30">
              <button class="btn button" @click="onUpdateStatus(orderAll.id, '40')">确认收货</button>
            </div> -->
            <!--去评价  -->
            <div class="t-c" v-if="orderAll.orderStatus == 40">
              <button class="btn button" @click="$router.push(`../../orderEvaluate?id=${orderAll.id}`)">去评价</button>
            </div>
          </td>
        </tr>
      </table>
    </div>
    <!--取消订单模态框  -->
    <div class="dialog-modal" v-show="isDelOrder" >
      <div class="bg"></div>
      <div class="modal">
        <!--关闭弹框按钮  -->
        <button type="button" class="btn-close transition-ease close" @click="closePopup()">
          <span class="iconfont ico-close"></span>
        </button>
        <div class="detail">
          <span class="tiltle">关闭原因</span>
          <div class="select">
            <w-select :options="options" v-model="refundSelected" ref="refundSelected"></w-select>
          </div>
        </div>
        <div class="detail">
          <span class="tiltle">备注</span>
          <textarea placeholder="内容在2-200字之间，建议填写取消订单相关信息" v-model="refundConent"></textarea>
        </div>
        <div class="btn-main">
          <w-button type="main" class="btnC" @on-click="commitDelOrder(orderAll.id)">提交</w-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import { apiEditOrderStatus, apiRefundOrderFormById, apiDeleteOrder, apiGoodsDetail } from 'js/api'
  import { mapGetters } from 'vuex'

  export default {
    props: {
      // 全部订单信息
      orderAll: {
        type: Object,
        default: () => { return {} }
      },
      options: {
        type: Array,
        default: () => { return [] }
      }
    },
    data() {
      return {
        goods_id: '',
        isDelOrder: false,
        refundConent: '', // 退款原因主要内容
        refundStatus: {}, // 退款商品原因
        goodsList: [],
        goodsNewList: [],
        listIndex: 0,
        len: 0,
        refundSelected: '' // 原因选择
        // 修改订单状态标识
        // pointsStatus: ''
      }
    },
    computed: {
      ...mapGetters(['token']),
      // 计算每个订单的不同商品sku的总数
      skuLen() {
        this.len = 0
        this.orderAll.goodsInfoList.forEach(list => {
          this.len += list.goodsGsp.length
        })
        return this.len
      },
      // 循环goodsInfoList
      goodsInfoList() {
        this.goodsList = []
        this.orderAll.goodsInfoList.forEach(list => {
          this.goodsList.push(list)
        })
        return this.goodsList
      },
      tabHeight() {
        return `${112 * this.skuLen}` + 'px'
      }
    },
    methods: {
      // 跳转商品详情
      goGoodsDetail(goodsId) {
        apiGoodsDetail(goodsId, this.token).then(data => {
          this.$router.push(`../../goods/detail/` + goodsId)
        })
      },
      // 跳转订单详情
      goOrderDetail(id) {
        this.goods_id = id
        this.$router.push(`/orderDetail?id=${id}`)
      },
      // 修改订单状态
      // onUpdateStatus(id, orderStatus) {
      //   let $this = this
      //   const orderId = id
      //   if (orderStatus == '40') {
      //     this.$modal({
      //       title: '提示',
      //       content: '您现在确认收货吗？',
      //       onConfirm() {
      //         $this.fetchEditOrderStatus($this.token, id, '40')
      //         $this.$toast('确认收货', () => {
      //           $this.$router.push(`../orderEvaluate?id=${orderId}`)
      //         })
      //       }
      //     })
      //   }
      // },
      /**
       * 关闭弹框
       */
      closePopup() {
        // 展示加载动画
        this.isDelOrder = false
        // 清空理由
        this.refundSelected = ''

        // 清空备注
        this.refundConent = ''

        // 清空图片
        this.redundPic = []

        // 让父级销毁此组件
        setTimeout(() => {
          this.$emit('close')
        }, 400)
      },
      fetchEditOrderStatus(token, id, orderStatus) {
        apiEditOrderStatus(token, id + '', orderStatus).then((data) => {
          // 让上级组件刷新列表
          this.$emit('on-refresh')
        })
      },
      // 取消订单
      delOrder() {
        this.isDelOrder = !this.isDelOrder // 模特框显示
      },
      // 取消订单模态框的提交
      commitDelOrder(orderId) {
        if (!this.$refs.refundSelected.value) {
          this.$toast('请选择退款原因')
        } else {
          apiRefundOrderFormById(this.token, orderId, this.$refs.refundSelected.value, this.refundConent, this.redundPic, 2).then(data => {
            this.$emit('on-refresh') // 刷新myOrder页面
          })
          // 清空退款原因
          this.refundSelected = ''
          this.refundConent = ''
          this.isDelOrder = !this.isDelOrder
        }
      },
      // 删除订单
      deleteOrder(orderId) {
        let $this = this
        this.$modal({
          content: '您确认删除订单吗？',
          onConfirm() {
            apiDeleteOrder([orderId]).then(data => {
              $this.$toast('订单删除成功', () => {
                $this.$emit('on-refresh') // 订单删除后，刷新myOrder页面
              })
            })
          }
        })
      },
      // 联系店家
      onContactShop(storeId, goodsId, goodName, goodImg) {
        const _this = this
        this.$store.dispatch('fetchUserRole', {
          token: this.$store.getters.token,
          callback(role) {
            _this.$store.dispatch('fetchCustomerByStoreId', {
              mobile: _this.$store.getters.mobile,
              storeId: storeId,
              goodsId: goodsId,
              firstMsg: JSON.stringify({
                'id': goodsId,
                'goods_main_photo': goodImg,
                'goodsName': goodName
              })
            })
            // if (role != 2) {
            //   _this.$toast('您不是采购商，只有采购商才可以使用聊天功能')
            // } else {
            //   _this.$store.dispatch('fetchCustomerByStoreId', {
            //     mobile: _this.$store.getters.mobile,
            //     storeId: storeId,
            //     goodsId: goodsId,
            //     firstMsg: JSON.stringify({
            //       'id': goodsId,
            //       'goods_main_photo': goodImg,
            //       'goodsName': goodName
            //     })
            //   })
            // }
          }
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
  @import '../../../scss/_var.scss';
  tr.order-list {
    td {
      width: 12.5%;

      &:nth-child(1) {
        width: 37.5%;
      }
      &:nth-child(2), &:nth-child(3) {
        text-align: center;
      }
    }
  }
  // 删除订单按钮
  .delBtn {
    width: 22px;
    height: 25px;
    display: inline-block;
    vertical-align: middle;
    background-image: url('../../../assets/btn_delete.png');
    background-repeat: no-repeat;
    background-size: cover;
    position: absolute;
    right: 20px;
    margin-top: 10px;
    &:hover {
      cursor: pointer;
    }
  }
  table{
    margin-top: 20px;
    // margin-bottom: 7px;
    background-color: $color-white;
    width: 100%;
    // border: 1px solid #ddd;
    color: #3c3c3c;
    tr{
      width: 100%;
      font-size: 0;
    }
    th{
      height: 40px;
      line-height: 40px;
      font-size: 14px;
      border-bottom: 1px solid #ddd;
      padding-left: 20px;
      padding-right: 20px;
      color: #3c3c3c;
        .c-h-l {
          display: inline-block;
          float: left;
          font-size: 12px;
          span {
            margin-right: 17px;
          }
        }
        .c-h-r {
          display: inline-block;
          vertical-align: middle;
          // margin: 0 50px 0 88px;
          margin-right: 274px;
          float: right;
          .store-name {
            max-width: 300px;
            margin-right: 17px;
            text-overflow: ellipsis;
            overflow: hidden;
            white-space: nowrap;
            float: left;
            color: $color-dark;
          }
          .connect-user {
            color: #ef0487;
            cursor: pointer;
            font-size: 20px;
          }
          .call-store {
            width: 110px;
            height: 29px;
            line-height: 27px;
            border: 1px solid;
            border-radius: 3px;
            color: $color-main;
            display: inline-block;
            cursor: pointer;
          }
        }
    }
    td{
      font-size: 14px;
      vertical-align: middle;
    }
    .cols1{
      padding: 15px 0 15px 20px;
      font-size: 12px;
        .shopImg {
          width: 80px;
          height: 80px;
          margin-right: 9px;
          float: left;
          cursor: pointer;
        }
        .shop-r {
          // float: left;
          height: 80px;
          width: 180px;
          padding: 0;
          margin-left: 85px;
          &-t {
            height: 40px;
            margin-bottom: 26px;
            color: #444;
            text-overflow: ellipsis;
            overflow: hidden;
            white-space: nowrap;
            line-height: 19px;
            .goods-type{
                color:$color-white;
                background: $color-main;
                padding:2px 5px;
                &:hover {
                  cursor: pointer;
                  color: #fff;
                }
              }
            span {
              &:hover {
                cursor: pointer;
                color: $color-main;
              }
            }
          }
          &-b {
            color: $color-dark;
            &-l {
              float: left;
            }
            &-r {
              float: right;
            }
          }
        }
    }
    .t-c{
      text-align: center;
    }
    .td-width {
      width: 134px; 
      display: inline-block
    }
  }
  .goDetail:hover{
    color: $color-main;
    cursor: pointer;
  }
  .button{
    background-color: $color-main;
    width: 80px;
    height: 30px;
    color: #fff;
  }
  .text-c {
    color: $color-main;
  }
  .text-c2 {
    color: #666;
  }
  // 取消订单
  .del-order {
    cursor: pointer;
  }
  // 模态框
  .dialog-modal {
    .bg {
      background-color: #000;
      opacity: 0.4;
      position: fixed;
      z-index: 8;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
    }
    .modal {
      width: 462px;
      height: auto;
      background-color: $color-white;
      position: fixed;
      z-index: 9;
      top: 25%;
      left: 0;
      right: 0;
      margin: 0 auto;
      padding: 33px 60px;
      .tiltle {
        width: 56px;
        text-align: right;
        color: $color-disabled;
        font-size: 14px;
        margin-right: 33px;
        vertical-align: top;
        display: inline-block;
        margin-top: 5px;
      }
      .detail {
        margin-bottom: 20px;
        .select {
          width: 150px;
          display: inline-block;
        }
        textarea {
          width: 248px;
          display: inline-block;
          resize: none;
          overflow: hidden;
          min-height: 90px;
          padding: 3px 10px;
          &::-moz-placeholder {
            font-size: 14px;
            color: $color-disabled;
          }
        }
        .upload {
          width: 248px;
          display: inline-block;
        }
      }
      .btn-main {
        margin-top: 10px;
        text-align: center;
        .btnC {
          width: 231px;
          background-color: $color-main;
          color: $color-white;
        }
      }
    }
  }
  .close {
    position: absolute;
    top: 10px;
    right: 10px;
  }
</style> 